home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_4.3 / XRG / XRG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  3.0 KB  |  140 lines

  1. /* 
  2.  * xrg.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /*
  10.  * XRG (Region Growing)
  11.  *
  12.  *  Program to take a binary image file of regions
  13.  *  along with a list of interior points for each
  14.  *  region and calculate the area of each region
  15.  *  by pixel filling using the interior point as
  16.  *  the starting point
  17.  */
  18.  
  19. #include "xrg.h"
  20.  
  21.  
  22. #define     ON        1
  23. #define     OFF        0
  24.  
  25. /*
  26.  * global variables
  27.  */
  28. extern char *optarg;
  29. extern int optind, opterr;
  30. extern short tiffInput;         /* flag=0 if no ImageIn to set tags; else =1 */
  31.  
  32.  
  33. /*
  34.  * usage of routine
  35.  */
  36. void
  37. usage (char *progname)
  38. {
  39.   progname = last_bs (progname);
  40.   printf ("USAGE: %s inimg point_file [-L]\n", progname);
  41.   printf ("\n%s calculates the area of each region in a binary image\n", progname);
  42.   printf ("by pixel filling using an interior point as\n");
  43.   printf ("the starting point.\n\n");
  44.   printf ("ARGUMENTS:\n");
  45.   printf ("        inimg: input image filename (TIF)\n");
  46.   printf ("   point_file: list of centroid coordinates\n");
  47.   printf ("               as generated by spp (.vin)\n\n");
  48.   printf ("OPTIONS:\n");
  49.   printf ("           -L: print Software License for this module\n");
  50.   exit (1);
  51. }
  52.  
  53. void
  54. main (int argc, char *argv[])
  55. {
  56.   Image *imgIn;
  57.  
  58.   int i_arg;
  59.   char *vin_file;
  60.   FILE *fpIn;
  61.   float xmin, xmax, ymin, ymax;
  62.   int nsites;
  63.   float fx, fy;
  64.   int x, y;
  65.   long n_pix;
  66.   int i;
  67.  
  68. /*
  69.  * cmd line options
  70.  */
  71.   static char *optstring = "L";
  72.  
  73.  
  74. /*
  75.  * parse command line
  76.  */
  77.   optind = 3;                   /* set getopt to point to the 3rd arg */
  78.   opterr = ON;                  /* give error messages */
  79.  
  80.  
  81.   if (argc < 3)
  82.     usage (argv[0]);
  83.  
  84.   while ((i_arg = getopt (argc, argv, optstring)) != EOF) {
  85.     switch (i_arg) {
  86.     case 'L':
  87.       print_sos_lic ();
  88.       exit (0);
  89.     default:
  90.       printf ("\ngetopt: unknown condition encountered\n");
  91.       exit (1);
  92.       break;
  93.     }
  94.   }
  95.  
  96. /*
  97.  * Read input image
  98.  */
  99.   imgIn = ImageIn (argv[1]);
  100.  
  101.   if (imgIn->bps == 8 && imgIn->spp == 3) {
  102.     printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
  103.     exit (1);
  104.   }
  105.  
  106. /*
  107.  * Draw a 255 border around image to eliminate
  108.  * edge effects for line fills
  109.  */
  110.   draw_rect (0, 0, imgIn->width - 1, imgIn->height - 1, imgIn, WHITE);
  111.  
  112.   vin_file = argv[2];
  113.  
  114.   printf ("read data from file %s\n", vin_file);
  115.   /* get size of data set from file */
  116.   if ((fpIn = fopen (vin_file, "r")) == NULL) {
  117.     printf ("\n...cannot open input file %s\n", vin_file);
  118.     exit (1);
  119.   }
  120.   if (fscanf (fpIn, "%d %f %f %f %f", &nsites, &xmin, &xmax, &ymin, &ymax) != 5) {
  121.     printf ("Format error in %s\n", vin_file);
  122.     fclose (fpIn);
  123.     exit (1);
  124.   }
  125.  
  126.   for (i = 0; i < nsites; i++) {
  127.     if (fscanf (fpIn, "%f %f", &fx, &fy) != 2) {
  128.       printf ("Format error in %s\n", vin_file);
  129.       fclose (fpIn);
  130.       exit (1);
  131.     }
  132.     x = (int) fx;
  133.     y = (int) fy;
  134.     n_pix = gdImageFill (x, y, imgIn, WHITE);
  135.     printf ("Area filled for point at (%d, %d) = %ld\n", x, y, n_pix);
  136.   }
  137.  
  138.   fclose (fpIn);
  139. }
  140.